//
//  StatusCell.swift
//  XinLangWeibo10
//
//  Created by 白利兵 on 15/10/20.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
/// 微博 Cell 中控件的间距数值®
let StatusCellMargin: CGFloat = 12
let PictureMargin: CGFloat = 8
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35
class StatusCell: UITableViewCell {
    
    //var status: Status
    // 懒加载控件
    lazy var topView: StatusCellTopView = StatusCellTopView()
    lazy var contentLabel: UILabel = UILabel(title: "微博正文", fontSize: 15, color: UIColor.darkGrayColor(), screenInsets: 12)
    lazy var pictureView: StatusPictureView = StatusPictureView()
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    // 微博视图模型
    var statusViewModel: StatusViewModel? {
        didSet {
            topView.viewModle = statusViewModel
            contentLabel.text = statusViewModel?.status.text
            pictureView.viewModel = statusViewModel
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
            }
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 根据视图模型，动态改变行高
    func changeRowHeight(vm: StatusViewModel) -> CGFloat {
        // 1. 重写赋值模型
        statusViewModel = vm
        // 2. 强制刷新约束
        contentView.layoutIfNeeded()
        // 3. 返回行高
        return CGRectGetMaxY(bottomView.frame)
    }
}

extension StatusCell {
     func setupUI() {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        // 设置数据
        // 设置布局
        // 1> 顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        // 2> 内容标签
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
        }
        // 3> 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            

          //   指定向下的约束
          //make.bottom.equalTo(contentView.snp_bottom)
    }
  }

}













